package com.zd.zjfx.util;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;


public class Lq_Security {

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private static final Charset ENCODING = StandardCharsets.UTF_8;
    public static final String ALGORITHM_NAME = "SM4";
    // 加密算法/分组加密模式/分组填充方式
    // PKCS5Padding-以8个字节为一组进行分组加密
    // 定义分组加密模式使用：PKCS5Padding
    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
   

    /**
     * 生成ECB暗号
     *
     * @param algorithmName 算法名称
     * @param mode          模式
     * @param key           密码
     * @explain ECB模式（电子密码本模式：Electronic codebook）
     */
    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
        Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
        cipher.init(mode, sm4Key);
        return cipher;
    }

    /**
     * sm4加密
     *
     * @param hexKey   16进制密钥（忽略大小写）
     * @param paramStr 待加密字符串
     * @return 返回Base64后加密字符串
     * @explain 加密模式：ECB
     */
    public static String encryptEcb(String hexKey, String paramStr) throws Exception {
        // 16进制字符串-->byte[]
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        // String-->byte[]
        byte[] srcData = paramStr.getBytes(ENCODING);
        // 加密后的数组
        byte[] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
        // byte[]-->hexString
        String cipherText = Base64.getEncoder().encodeToString(cipherArray);
        return cipherText;
    }

    /**
     * 加密模式为ECB
     *
     * @param key  2进制密钥
     * @param data 2进制原文
     * @return 二进制密文
     */
    public static byte[] encrypt_Ecb_Padding(byte[] key, byte[] data) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    /**
     * sm4解密
     *
     * @param hexKey     16进制密钥
     * @param cipherText 16进制的加密字符串（忽略大小写）
     * @return 解密后的字符串
     * @explain 解密模式：采用ECB
     */
    @SuppressWarnings("UnnecessaryLocalVariable")
    public static String decryptEcb(String hexKey, String cipherText) throws Exception {
        // hexString-->byte[]
        byte[] keyData = ByteUtils.fromHexString(hexKey);
        // hexString-->byte[]
        byte[] cipherData = Base64.getDecoder().decode(cipherText);
        // 解密
        byte[] srcData = decrypt_Ecb_Padding(keyData, cipherData);
        // 接收解密后的字符串 byte[]-->String
        String decryptStr = new String(srcData, ENCODING);
        return decryptStr;
    }

    /**
     * sm4解密
     *
     * @param key  2进制密钥
     * @param cipherText 2进制密文
     * @return 解密后的2进制原文
     */
    public static byte[] decrypt_Ecb_Padding(byte[] key, byte[] cipherText) throws Exception {
        Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(cipherText);
    }
    
    public static void main(String args []) throws Exception {
    	
    	//String aaa=encryptEcb("9904033e1837094cfe11772fc2d90011","{\"nsrsbh\":\"911305005809845195\"}");
    	//
    	//String aaa=ZipBase64.unZipString("UEsDBBQACAgIADWiN1UAAAAAAAAAAAAAAAAsAAAAZHpmcF8yMjQ0MjAwMDAwMDAwMjA0MDI2MV8yMDIyMDkyMzIwMTc0Mi54bWzFWFtT20YUfs+v8PgxY2xJvmeEMwac4mlIUzDJ9FGxF9DUlqktpTDTB5MEEi4JpLihCQRC0ibMFBKScjEGkj+jlex/0aOLLcmsDc10phpGXp3vnLNnz2X3LOzViVzWdRcVinxe6HbTXsrtQkI6n+GF0W63JI50RdyuosgJGS6bF1C3exIV3Vdjl9hEUrib59ModskFD9uPuAwqGB86IZHkMzGGCQQYynwYKkAx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e125V/w9Jy2jqGo90uatM/7PH2/85YaouKK84IgkoPH4Un3x/lLsv0ietulz2jvTfCtE64eRwdnwddNMSdWTcepKpjpixRryMG2zWCjzNCdB3ilTA0lijHSbSkGZZG+cgJX3K453BZ+2Bwet8aYe/f1hzXORZ93wtdtynk54V89yVd4opxX8eKMTWiwfCW5cP3Gzdm5IpDk9JvydUNT4aQgsfM+DU/fB4N+uDsg7T7Z8zm3AOEEee6H6x+HnaecN6oT9POt6LDPuUW8jJVLMS+k5P6JjocALT6CUkOs1+1EeU0CgJWLHBVxEwB5ZkO1i1i1GA4+vaPANpJUR51B9KqX5YlNzUKcIa01srMatrfIRifq+ZxP48ZS7l8u5KU4Paw/YhjkpQJgAaEFHaxbmpV15TB6chSu75KzZtR9RjZOqOfvfhJiLAWoWRhrOlSUyzNM++FVaAA0VuMsqxnDrAsJVPnekv1B8KVP2mth6zQOoumuZjLmLoUL5vISrwbI8owkz8hzAgDz7OHdsiHOKocI/4pbYIJ4urqtGpnGhukijZo+rXx8kWz8++Wj9nTB6E+k2ZtYMMqnU8votGuU+9uCUcmVC6aZpnuLV8DScV2MXLQSm2KDl5vekOyGFKc3ZHpDpjfkv29I5Sd6I2uxiQgAAA==");
    	System.out.println(aaa);
    	
    }
}


